package com.ylfstu.list_;

import com.sun.security.jgss.GSSUtil;

import java.util.ArrayList;
import java.util.List;

public class ListExercise02 {
    @SuppressWarnings({"all"})
    public static void main(String[] args) {
        List list = new ArrayList();
        list.add(new Book("红楼梦", "曹雪芹", 59));
        list.add(new Book("西游记", "吴承恩", 49));
        list.add(new Book("水浒传", "施耐庵", 39));
        list.add(new Book("三国", "罗贯中", 9));
//        System.out.println(list);
        for (Object book :list) {
            System.out.println(book);
        }
        sort(list);
        for (Object book :list) {
            System.out.println(book);
        }


    }
    @SuppressWarnings({"all"})
    public static void sort(List list) {

        int listSize = list.size();
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = 0; j < list.size() - 1 - i; j++) {
                //取出Book对象
                Book book1 = (Book)list.get(j);
                Book book2 = (Book)list.get(j + 1);
                if (book1.getPrice() > book2.getPrice()) {
                    list.set(j, book2);
                    list.set(j + 1, book1);
                }
            }
        }
    }
}
class Book{
    private String name;
    private String author;
    private double price;

    @Override
    public String toString() {
        return "名称：" + name + "\t\t价格：" + price + "\t\t作者：" + author;
    }

    public Book(String name, String author, double price) {
        this.name = name;
        this.author = author;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }
}
